package com.drinks.yiniang.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.listview.PayPasswordKeyBoardAdapter;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.ui.activity.user.setting.ForgotPaymentPasswordActivity;
import com.drinks.yiniang.ui.widget.textview.PayPasswordView;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.widget.dialog.BaseDialogFragment;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 输入支付密码dialog
 * Created by han on 2018/10/30 0030.
 */

public class PayPasswordDialog extends BaseDialogFragment implements View.OnClickListener,
        PayPasswordView.OnPasswordInputCompleteListener, AdapterView.OnItemClickListener {

    private final static int NUMBER_BUTTON_DELETE = 11;//删除
    private final static int NUMBER_BUTTON_ZERO = 10;//0号按键
    private final static int NUMBER_BUTTON_CLEAR = 9;//清除按键

    private Context mContext;

    private ImageView iv_pay_pwd_close;
    //输入密码框
    private PayPasswordView tv_pay_pwd;
    //忘记密码
    private TextView tv_pay_forgot_pwd;
    //输入键盘
    private GridView gv_pay_pwd_keyboard;

    private OnPayPasswordListener onPayPasswordListener;

    private PayPasswordKeyBoardAdapter mPwdKeyBoardAdapter;

    public static PayPasswordDialog newInstance() {
        PayPasswordDialog dialogFragment = new PayPasswordDialog();

        return dialogFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            onPayPasswordListener = (OnPayPasswordListener) context;
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + " must implement OnPayListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mContext = getActivity();

        LayoutInflater inflater = this.getActivity().getLayoutInflater();

        View mView = inflater.inflate(R.layout.dialog_pay_password, null);

        Dialog dialog = new Dialog(this.getActivity(), R.style.PayDialogStyle);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(mView);
        dialog.setCanceledOnTouchOutside(false);

        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.dimAmount = 0.5f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平

        dialogWindow.setAttributes(lp);
        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        iv_pay_pwd_close = mView.findViewById(R.id.iv_pay_pwd_close);

        tv_pay_pwd = mView.findViewById(R.id.tv_pay_pwd);
        tv_pay_forgot_pwd = mView.findViewById(R.id.tv_pay_forgot_pwd);

        gv_pay_pwd_keyboard = mView.findViewById(R.id.gv_pay_pwd_keyboard);

        iv_pay_pwd_close.setOnClickListener(this);
        tv_pay_forgot_pwd.setOnClickListener(this);
        tv_pay_pwd.setOnPasswordCompleteLinstener(this);
        gv_pay_pwd_keyboard.setOnItemClickListener(this);

        mPwdKeyBoardAdapter = new PayPasswordKeyBoardAdapter(mContext);

        gv_pay_pwd_keyboard.setAdapter(mPwdKeyBoardAdapter);

        return dialog;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_pay_pwd_close://关闭
                if (onPayPasswordListener != null) {
                    onPayPasswordListener.onPayPasswordClose();
                }
                dismiss();
                break;
            case R.id.tv_pay_forgot_pwd://忘记密码
                mContext.startActivity(new Intent(mContext, ForgotPaymentPasswordActivity.class));
                break;
            default:
                break;
        }
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (onPayPasswordListener != null) {
            onPayPasswordListener.onPayPasswordDismiss();
        }
    }

    @Override
    public void dismiss() {
        //super.dismiss();
        dismissAllowingStateLoss();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String password = tv_pay_pwd.getText().toString();

        if (position == NUMBER_BUTTON_CLEAR) {
            password = "";
        } else if (position == NUMBER_BUTTON_DELETE) {
            if (password.length() > 0) {
                password = password.substring(0, password.length() - 1);
            }
        } else {
            if (position == NUMBER_BUTTON_ZERO) {
                password += "0";
            } else {
                password += (position + 1);
            }
        }

        tv_pay_pwd.setText(password);
    }

    @Override
    public void onComplete() {
        doPayPasswordVerify();
    }

    //验证支付密码
    private void doPayPasswordVerify() {
        final String password = StringUtils.getTextViewText(tv_pay_pwd.getText());

        if (StringUtils.isEmpty(password) || password.length() != 6) {
            ToastUtils.showShort(R.string.text_hint_input_payment_password);
            return;
        }

        showLoadingDialog();

        UserApiIO.getInstance().doPayPasswordVerify(password, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                if (onPayPasswordListener != null) {
                    onPayPasswordListener.onPayPasswordSuccess(password);
                }
                dismiss();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    @Override
    public void onDestroy() {
        onPayPasswordListener = null;
        super.onDestroy();
    }

    public interface OnPayPasswordListener {
        void onPayPasswordSuccess(String password);

        void onPayPasswordClose();

        void onPayPasswordDismiss();
    }
}

